<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * size short summary.
 *
 * size description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Size extends Goods_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Size_model');
        $this->load->library('form_validation');
    }

    public function sizelist()
    {
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;

        $pagesize=10;
        $one=$this->Size_model->get_page($cupge,$pagesize,"*","size","order by size_id desc","size_id","");
        $this->load->library('page');
        $p=new Page();
        $data['sizeList']=arrayToObject($one['objlist']);
        $p->init($one['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/size/sizelist'));
        $data['links']=$p->showPages(1);
        $this->load->view('Admin/size/sizelist.html',$data);

    }

    public function addsize()
    {
        $this->load->view('Admin/size/addsize.html');
    }

    public function add_size()
    {
        $valiRes= $this->form_validation->run('size_vali');
        $this->load->library('ajaxMsgModel');
        $sizeModel= new Size_model();
        if($valiRes)
        {
            $sizeModel->title=$this->input->post('title');
            $res= $sizeModel->insert_size($sizeModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/size/sizelist");
            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function editsize()
    {
        $sizeId=$this->uri->segment(4);
    	$data['sizeModel']=$this->Size_model->get_size_by_id($sizeId);
        $this->load->view('Admin/size/editsize.html',$data);
    }

    public function edit_size()
    {
        $valiRes= $this->form_validation->run('size_vali');
        $this->load->library('ajaxMsgModel');
        $sizeModel= new Size_model();
        if($valiRes)
        {
            $sizeModel->size_id=$this->input->post('size_id');
            $sizeModel->title=$this->input->post('title');
            $res= $sizeModel->edit_size($sizeModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/size/sizelist");
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }



    public function del_size()
    {
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $res=$this->Size_model->del_size_byid($this->uri->segment(4));
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function batchdel_size()
    {
        $size_id=$this->input->post('items');
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $sizeModel=new $this->Size_model();
        $size_idArray=explode('|',trim($size_id,'|'));
        $res=0;
        if(count($size_idArray)>0)
        {
            $res=$sizeModel->batchdel_size($size_idArray);
        }
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}